package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.dao.INotInPaymentDao;
import com.boss.salary.dao.impl.AutoSubmitDaoImpl;
import com.boss.salary.entity.NotInPayment;
import com.boss.salary.service.INotInPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class NotInPaymentServiceImpl implements INotInPaymentService {
    @Autowired
    private INotInPaymentDao notInPaymentDao;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitNotIn(NotInPayment notInPayMent) {

        if (!CollectionUtils.isEmpty(notInPayMent.getProCatCodeList())){//项目分类
            notInPayMent.setProCatCode(String.join(",", notInPayMent.getProCatCodeList()));
        }
        if (!CollectionUtils.isEmpty(notInPayMent.getProCodeList())){ //预算项目
            notInPayMent.setProCode(String.join(",", notInPayMent.getProCodeList()));
        }
        if (!CollectionUtils.isEmpty(notInPayMent.getPayTypeCodeList())){//支付方式
            notInPayMent.setPayTypeCode(String.join(",", notInPayMent.getPayTypeCodeList()));
        }
        if (!CollectionUtils.isEmpty(notInPayMent.getDepBgtEcoCodeList())){//经济分类
            notInPayMent.setDepBgtEcoCode(String.join(",", notInPayMent.getDepBgtEcoCodeList()));
        }
        if (!CollectionUtils.isEmpty(notInPayMent.getPayCategoryCodeList())){ //工资类别
            notInPayMent.setPayCategoryCode(String.join(",", notInPayMent.getPayCategoryCodeList()));
        }
        notInPaymentDao.insertNotInPayment(notInPayMent);
    }

    @Override
    public void deleteNotInPayment(NotInPayment notInPayMent) {
        notInPaymentDao.deleteNotInPayment(notInPayMent);
    }

    @Override
    public PageDTO<NotInPayment> queryNotInPaymentPage(PaginationDTO paginationDTO, String province) throws AppRuntimeException {
        List<NotInPayment> list = notInPaymentDao.queryNotInPaymentPage(paginationDTO,province);
        int total = notInPaymentDao.queryNotInPaymentTotal(province);
        PageDTO<NotInPayment> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    @Override
    public String getNotInPaymentCondition(String mofDivCode) {
        List<NotInPayment> notInPayMentList = notInPaymentDao.selectNotInPayment(mofDivCode);

        if (CollectionUtils.isEmpty(notInPayMentList)) {
            return null;
        }
        StringBuffer condition = new StringBuffer();
        for(NotInPayment notIn:notInPayMentList){
            StringBuffer notIncondition = new StringBuffer();
        //项目分类
        String proCatCode = notIn.getProCatCode();//项目分类
        //预算项目
        String proCode = notIn.getProCode();//预算项目
        //支付方式
        String payTypeCode = notIn.getPayTypeCode();//支付方式
        //经济分类
        String depBgtEcoCode = notIn.getDepBgtEcoCode();//经济分类
        //工资类别
        String payCategoryCode = notIn.getPayCategoryCode();//工资类别

        if (!StringUtils.isEmpty(proCatCode)) {//项目分类
            notIncondition.append(" and ");
            if(notIncondition.indexOf("(")<0) {
                notIncondition.append(" (");
            }
            notIncondition.append(AutoSubmitDaoImpl.notInSql("pro_cat_code", CollectionUtils.arrayToList(proCatCode.split(","))));
        }
        if (!StringUtils.isEmpty(proCode)) {//预算项目
            notIncondition.append(" and ");
            if(notIncondition.indexOf("(")<0) {
                notIncondition.append(" (");
            }
            notIncondition.append(AutoSubmitDaoImpl.notInSql("pro_code", CollectionUtils.arrayToList(proCode.split(","))));
        }
        if (!StringUtils.isEmpty(payTypeCode)) {//支付方式
            notIncondition.append(" and ");
            if(notIncondition.indexOf("(")<0) {
                notIncondition.append(" (");
            }
            notIncondition.append(AutoSubmitDaoImpl.notInSql("pay_type_code", CollectionUtils.arrayToList(payTypeCode.split(","))));
        }
        if (!StringUtils.isEmpty(depBgtEcoCode)) {//经济分类
            notIncondition.append(" and ");
            if(notIncondition.indexOf("(")<0) {
                notIncondition.append(" (");
            }
            notIncondition.append(AutoSubmitDaoImpl.notInSql("dep_bgt_eco_code", CollectionUtils.arrayToList(depBgtEcoCode.split(","))));
        }
        if (!StringUtils.isEmpty(payCategoryCode)) {//工资类别
            notIncondition.append(" and ");
            if(notIncondition.indexOf("(")<0) {
                notIncondition.append(" (");
            }
            notIncondition.append(AutoSubmitDaoImpl.notInSql("pay_category_code", CollectionUtils.arrayToList(payCategoryCode.split(","))));
        }
            notIncondition.append(") ");
            condition.append(notIncondition);
    }
        return condition.toString();
    }
}
